package com.iraeta.service.iraorder.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iraeta.common.ServiceException;
import com.iraeta.dao.iraorder.IraorderproductDao;
import com.iraeta.dao.iraorder.IraorderscstepproductDao;
import com.iraeta.entity.Iraorderproduct;
import com.iraeta.entity.Iraorderscstepproduct;
import com.iraeta.entity.vo.IraOrderProductStep;
import com.iraeta.entity.vo.IraOrderProductStepCount;
import com.iraeta.service.iraorder.IraProductStepService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class IraProductStepServiceImpl implements IraProductStepService {
    @Resource
    IraorderscstepproductDao iraorderscstepproductDao;
    @Resource
    IraorderproductDao iraorderproductDao;

    @Override
    public void addIraorderscstepproduct(Iraorderscstepproduct iraorderscstepproduct) {
        iraorderscstepproductDao.insert(iraorderscstepproduct);
    }

    @Override
    public void updateIraorderscstepproduct(Iraorderscstepproduct iraorderscstepproduct) {
        iraorderscstepproductDao.updateByPrimaryKey(iraorderscstepproduct);
    }

    @Override
    public void deleteIraorderscstepproduct(Integer id) {
        iraorderscstepproductDao.deleteByPrimaryKey(id);
    }

    @Override
    public Iraorderscstepproduct getIraorderscstepproduct(Integer id) {
        return iraorderscstepproductDao.selectByPrimaryKey(id);
    }

    /*查询所有产品， 同事产品的所有步骤和步骤中的数量*/
    @Override
    public List<IraOrderProductStep> listIraOrderProductStep(Integer orderId) {
        List<Iraorderproduct> iraorderproductList = iraorderproductDao.selectByOrderId(orderId);
        List<IraOrderProductStep> iraOrderProductStepList = new ArrayList<>();
        for(Iraorderproduct iraorderproduct : iraorderproductList){
            IraOrderProductStep iraOrderProductStep = new IraOrderProductStep();
            BeanUtil.copyProperties(iraorderproduct, iraOrderProductStep);
            List<IraOrderProductStepCount> iraOrderProductStepCountList = iraorderscstepproductDao.selectProductStepCount(iraorderproduct.getId());
            iraOrderProductStep.setIraOrderProductStepCountList(iraOrderProductStepCountList);
            iraOrderProductStepList.add(iraOrderProductStep);
        }
        return iraOrderProductStepList;
    }

    @Transactional
    @Override
    public void saveIraOrderProductStep(JSONArray stepCountJSONArray) {
        for(int i=0;i<stepCountJSONArray.size();i++){
            JSONObject stepCountJSONObject = stepCountJSONArray.getJSONObject(i);
            int pid = stepCountJSONObject.getIntValue("pid");
            int sid = stepCountJSONObject.getIntValue("sid");
            int orderid = stepCountJSONObject.getIntValue("orderid");
            if(pid == 0 || sid == 0 || orderid==0){
                throw new ServiceException("error param");
            }else{
                int stepCount = stepCountJSONObject.getIntValue("stepCount");
                Iraorderscstepproduct iraorderscstepproductDb = iraorderscstepproductDao.selectByPS(pid, sid);
                if(iraorderscstepproductDb == null){
                    iraorderscstepproductDb = new Iraorderscstepproduct(orderid,sid,pid,stepCount);
                    iraorderscstepproductDao.insert(iraorderscstepproductDb);
                }else{
                    iraorderscstepproductDb.setStepcount(stepCount);
                    iraorderscstepproductDao.updateByPrimaryKey(iraorderscstepproductDb);
                }
            }
        }
    }
}
